package com.shangcheng.orders.controller;


import com.shangcheng.orders.service.CartService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 描述
 *
 * @author www.itheima.com
 * @version 1.0
 * @package com.changgou.order.controller *
 * @since 1.0
 */
@RestController
@RequestMapping("/cart")
@CrossOrigin
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 添加购物车
     *
     * @param id  要购买的商品的SKU的ID
     * @param num 要购买的数量
     * @return
     */
    @GetMapping(value = "/add")
    public Result add(@RequestParam Long id,
                      @RequestParam Integer num,
                      @RequestParam String username) {
        cartService.add(id, num, username);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @GetMapping(value = "/dele")
    public Result deleteCart(@RequestParam String username) {
        cartService.delete(username);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping(value = "/list")
    public Result<Map> list(@RequestParam String username) {
        Map map = cartService.list(username);
        return new Result<Map>(true, StatusCode.OK, "列表查询成功", map);
    }

}
